﻿Option Infer On 'Using LINQ

Public Class frmDeleteEmployee
    Private records ' This will be used to for the find

    Private Function FindRecord() As Boolean
        Dim SearchBy As String = cmbSearchBy.SelectedItem
        Dim searchKeyWord As String = txtSearch.Text
        lblResults.Text = "Searching..."

        Select Case SearchBy
            Case "EmployeeName"
                records = From employee In Me.Atlantic_toyota_atm_dbDataSet.employee _
                Where employee.EmployeeName Like searchKeyWord & "*" _
                Select employee

            Case "EmployeeID"
                records = From employee In Me.Atlantic_toyota_atm_dbDataSet.employee _
                Where employee.EmployeeID Like searchKeyWord & "*" _
                Select employee
        End Select

        Try
            Me.EmployeeBindingSource.DataSource = records
            lblCurrentPosition.Text = Me.EmployeeBindingSource.Position + 1 & _
                                      " of " & Me.EmployeeBindingSource.Count.ToString
            lblResults.Text = Me.EmployeeBindingSource.Count.ToString & " records found."


        Catch ex As Exception
            lblResults.Text = "No records found."
            ' Load data in textboxes
            Me.EmployeeTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.employee)
            ' Load data in textboxes
            Me.EmployeeTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.employee)
            ' Load data in textboxes
            Me.EmployeeTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.employee)
        End Try
    End Function

    Private Sub frmDeleteEmployee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Hide the labels for the options
        lblView.Visible = False
        lblAdd.Visible = False

        ' Hides all the main menu labels

        lblHome.Visible = False
        lblVisit.Visible = False
        lblSurvey.Visible = False
        lblStatistics.Visible = False
        lblInventory.Visible = False
        'lblEmployee.Visible = False
        lblClose.Visible = False

        ' Hides the find and delete this item labels

        lblFind.Visible = False
        lblDeleteThisItem.Visible = False

        ' Hides all the labels from the navigation menu

        lblFirst.Visible = False
        lblBack.Visible = False
        lblNext.Visible = False
        lblLast.Visible = False

        ' Add picture to the menu buttons

        btnView.Image = GeneralImageSet.Images("Info")
        btnDelete.Image = GeneralImageSet.Images("Delete")
        btnAdd.Image = GeneralImageSet.Images("AddNew")

        ' Add picture to the navigation set

        btnFirst.Image = NavigationImageSet.Images("First")
        btnBack.Image = NavigationImageSet.Images("Back")
        btnNext.Image = NavigationImageSet.Images("Forward")
        btnLast.Image = NavigationImageSet.Images("Last")

        ' Add picture for the specific funcionality

        btnDeleteThisItem.Image = GeneralImageSet.Images("DeleteItem")
        btnFind.Image = GeneralImageSet.Images("Search")

        EmployeeIDTextBox.ReadOnly = True
        EmployeeNameTextBox.ReadOnly = True

        'TODO: This line of code loads data into the 'Atlantic_toyota_atm_dbDataSet.employee' table. You can move, or remove it, as needed.
        Me.EmployeeTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.employee)
        'TODO: This line of code loads data into the 'Atlantic_toyota_atm_dbDataSet.employee' table. You can move, or remove it, as needed.
        Me.EmployeeTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.employee)

        lblCurrentPosition.Text = System.Convert.ToString(Me.EmployeeBindingSource.Position + 1 & _
                                                          " of " & Me.EmployeeBindingSource.Count)

        cmbSearchBy.SelectedItem = "EmployeeName"
        txtSearch.Focus()


    End Sub

    Private Sub LoadNewValues(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.VisibleChanged
        lblResults.Text = ""
        txtSearch.Text = ""

        ' Load data in textboxes
        Me.EmployeeTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.employee)
        ' Load data in textboxes
        Me.EmployeeTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.employee)
        ' Load data in textboxes
        Me.EmployeeTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.employee)

        lblCurrentPosition.Text = System.Convert.ToString(Me.EmployeeBindingSource.Position + 1 & _
                                                  " of " & Me.EmployeeBindingSource.Count)
    End Sub

    '*************************************************
    ' Show the label for the items on the Navigation *
    '*************************************************

    Private Sub btnBack_MouseOver(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles btnBack.MouseHover
        btnBack.Image = NavigationImageSet.Images("BackSelected")
        lblBack.Visible = True
    End Sub

    Private Sub btnLast_MouseOver(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles btnLast.MouseHover
        btnLast.Image = NavigationImageSet.Images("LastSelected")
        lblLast.Visible = True
    End Sub

    Private Sub btnNext_MouseOver(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles btnNext.MouseHover
        btnNext.Image = NavigationImageSet.Images("ForwardSelected")
        lblNext.Visible = True
    End Sub

    Private Sub btnFirst_MouseOver(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles btnFirst.MouseHover
        btnFirst.Image = NavigationImageSet.Images("FirstSelected")
        lblFirst.Visible = True
    End Sub

    Private Sub btnFind_MouseOver(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnFind.MouseHover
        lblFind.Visible = True
    End Sub

    Private Sub btnDeleteThisItem_MouseOver(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnDeleteThisItem.MouseHover
        lblDeleteThisItem.Visible = True
    End Sub


    '******************************************************
    ' Hide the label for the items on the main Navigation *
    '******************************************************

    Private Sub btnBack_MouseLeave(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles btnBack.MouseLeave
        btnBack.Image = NavigationImageSet.Images("Back")
        lblBack.Visible = False
    End Sub

    Private Sub btnLast_MouseLeave(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles btnLast.MouseLeave
        btnLast.Image = NavigationImageSet.Images("Last")
        lblLast.Visible = False
    End Sub

    Private Sub btnNext_MouseLeave(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnNext.MouseLeave
        btnNext.Image = NavigationImageSet.Images("Forward")
        lblNext.Visible = False
    End Sub

    Private Sub btnFirst_MouseLeave(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles btnFirst.MouseLeave
        btnFirst.Image = NavigationImageSet.Images("First")
        lblFirst.Visible = False
    End Sub

    Private Sub btnFind_MouseLeave(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles btnFind.MouseLeave
        lblFind.Visible = False
    End Sub

    Private Sub btnDeleteThisItem_MouseLeave(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnDeleteThisItem.MouseLeave
        lblDeleteThisItem.Visible = False
    End Sub



    '************************************************
    ' Show the label for the items on the main menu *
    '************************************************

    Private Sub btnHome_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnHome.MouseHover
        lblHome.Visible = True
    End Sub

    Private Sub btnVisit_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnVisit.MouseHover
        lblVisit.Visible = True
    End Sub

    Private Sub btnSurvey_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnSurvey.MouseHover
        lblSurvey.Visible = True
    End Sub

    Private Sub btnStatistics_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnStatistics.MouseHover
        lblStatistics.Visible = True
    End Sub

    Private Sub btnInventory_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnInventory.MouseHover
        lblInventory.Visible = True
    End Sub

    Private Sub btnClose_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnClose.MouseHover
        lblClose.Visible = True
    End Sub


    '**************************************
    ' Hides the labels from the main menu *
    '**************************************


    Private Sub btnHome_MouseLeave(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles btnHome.MouseLeave
        lblHome.Visible = False
    End Sub

    Private Sub btnVisit_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnVisit.MouseLeave
        lblVisit.Visible = False
    End Sub

    Private Sub btnSurvey_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnSurvey.MouseLeave
        lblSurvey.Visible = False
    End Sub

    Private Sub btnStatistics_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnStatistics.MouseLeave
        lblStatistics.Visible = False
    End Sub

    Private Sub btnInventory_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnInventory.MouseLeave
        lblInventory.Visible = False
    End Sub

    Private Sub btnClose_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnClose.MouseLeave
        lblClose.Visible = False
    End Sub


    '**************************************
    ' Shows the labels from the main menu *
    '**************************************

    Private Sub btnView_MouseOver(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) Handles btnView.MouseHover
        lblView.Visible = True
    End Sub

    Private Sub btnAdd_MouseOver(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) Handles btnAdd.MouseHover
        lblAdd.Visible = True
    End Sub


    '**************************************
    ' Hides the labels from the main menu *
    '**************************************

    Private Sub btnView_MouseLeave(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) Handles btnView.MouseLeave
        lblView.Visible = False
    End Sub

    Private Sub btnAdd_MouseLeave(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) Handles btnAdd.MouseLeave
        lblAdd.Visible = False
    End Sub


    ' Finds a record when the button is clicked

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        FindRecord()
    End Sub

    ' Closes all forms

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        CloseAllForms()
    End Sub

    '*****************************************
    ' Move pointers of the navigation system *
    '*****************************************

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        EmployeeBindingSource.MoveFirst()
        lblCurrentPosition.Text = Me.EmployeeBindingSource.Position + 1 & _
                          " of " & Me.EmployeeBindingSource.Count.ToString
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        EmployeeBindingSource.MoveLast()
        lblCurrentPosition.Text = Me.EmployeeBindingSource.Position + 1 & _
                          " of " & Me.EmployeeBindingSource.Count.ToString
    End Sub

    Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
        EmployeeBindingSource.MovePrevious()
        lblCurrentPosition.Text = Me.EmployeeBindingSource.Position + 1 & _
                          " of " & Me.EmployeeBindingSource.Count.ToString
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        EmployeeBindingSource.MoveNext()
        lblCurrentPosition.Text = Me.EmployeeBindingSource.Position + 1 & _
                          " of " & Me.EmployeeBindingSource.Count.ToString
    End Sub

    ' Deletes Selected Item

    Private Sub btnDeleteThisItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteThisItem.Click
        Try
            Dim result As MsgBoxResult
            Dim Count As String = Me.EmployeeBindingSource.Count.ToString

            result = MessageBox.Show("Are you sure you want to delete " & _
                            EmployeeNameTextBox.Text & "? This cannot be undone.", _
                            "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
            If result = MsgBoxResult.Yes Then
                ' Something goes here

                Me.Validate()
                Me.Atlantic_toyota_atm_dbDataSet.BeginInit()
                EmployeeTableAdapter.DeleteQuery(EmployeeIDTextBox.Text)
                EmployeeTableAdapter.Update(Atlantic_toyota_atm_dbDataSet)
                'Me.EmployeeBindingSource.RemoveCurrent()
                Me.EmployeeBindingSource.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.Atlantic_toyota_atm_dbDataSet)
                Me.Atlantic_toyota_atm_dbDataSet.EndInit()
                Me.Atlantic_toyota_atm_dbDataSet.AcceptChanges()
                lblResults.Text = "Record Deleted."

                ' Load data in textboxes
                Me.EmployeeTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.employee)
                ' Load data in textboxes
                Me.EmployeeTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.employee)
                ' Load data in textboxes
                Me.EmployeeTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.employee)

                FindRecord()
                lblCurrentPosition.Text = System.Convert.ToString(Me.EmployeeBindingSource.Position + 1 & _
                                                          " of " & Me.EmployeeBindingSource.Count)

            End If

            'Me.EmployeeBindingSource.DataSource = Me.Atlantic_toyota_atm_dbDataSet
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    ' Switch Forms

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        SwitchForm(My.Forms.frmAddEmployee)
    End Sub

    Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click
        SwitchForm(My.Forms.frmViewEmployee)
    End Sub

    '***************************************
    ' Control the buttons of the main menu *
    '***************************************

    Private Sub btnClose_Visit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Dim Confirmation As MsgBoxResult
        Confirmation = MessageBox.Show("Unsaved changes will be lost. Are you sure you want to exit?", _
                                       "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If Confirmation = MsgBoxResult.Yes Then
            CloseAllForms()
        End If
    End Sub

    Private Sub btnHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHome.Click
        SwitchForm(My.Forms.frmMain)
    End Sub

    Private Sub btnVisit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVisit.Click
        SwitchForm(My.Forms.frmQuickVisit)
    End Sub

    Private Sub btnSurvey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSurvey.Click
        SwitchForm(My.Forms.frmSurvey)
    End Sub

    Private Sub btnStatistics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatistics.Click
        SwitchForm(My.Forms.frmStatistics)
    End Sub

    Private Sub btnInventory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInventory.Click
        SwitchForm(My.Forms.frmViewInventory)
    End Sub

    Private Sub btnEmployee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmployee.Click
        SwitchForm(My.Forms.frmViewEmployee)
    End Sub
End Class